package com.zrkj.util;

import cn.hutool.core.io.file.FileReader;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

@Slf4j
public class FileUtil {

    /**
     * 根据文件路劲 把文件称String
     *
     * @param fileUrl
     * @return
     */
    public static String fileToString(String fileUrl) {
        FileReader fileReader = new FileReader(fileUrl);
        String result = fileReader.readString();
        return result;
    }


    /**
     * 获取非结构化平台文件路径
     * @param uuid
     * @return
     */
//    public static String getFileUrl(String uuid) {
//        String robourl = "http://26.47.136.14:20005";
//        RobocopClient client = RobocopClientFactory.getRobocopClient("zfznfzpt","i68735R2rP", robourl);
//        String url = null;
//        try {
//            url = client.getDownloadUrl(uuid);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("获取非结构化平台文件路径失败,{}", e.getMessage());
//        }
//        return url;
//    }

    /**
     * 根据URL获取json内容
     *
     * @param url
     * @return
     */
    public static String loadJsonByUrl(String url) {

        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL(url);
            URLConnection urlConnection = urlObject.openConnection();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), StandardCharsets.UTF_8));
            String inputLine = null;
            while ((inputLine = bufferedReader.readLine()) != null) {
                json.append(inputLine);
            }
            bufferedReader.close();

        } catch (MalformedURLException e) {
            e.printStackTrace();
            log.error("根据URL获取json内容失败,{}", e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            log.error("根据URL获取json内容失败,{}", e.getMessage());
        }
        String returnStr = json.toString();
//        int begin = returnStr.indexOf("{");
//        int end = returnStr.lastIndexOf("}") + 1;
//        returnStr = returnStr.substring(begin, end);
        return returnStr;
    }

    public static void main(String[] args) {
        String s = FileUtil.fileToString("C:\\Users\\七分宁日\\Desktop\\JSON\\FT20210603001.json");
        System.out.println(s);
    }
}
